2-2 ϥΦh֤ߨӥ[tB

¦¹¥~¡A§Ú­Ì¤]¥i¥H­É¥Î¦h®Ö¹q¸£ªº¥­¦æ³B²z¡A¨Ó¥[³tµ{¦¡½Xªº°õ¦æ¡C±ý¹F¨ì¦¹¥\¯à¡A§Aªº¹q¸£¥²¶·¨ã³Æ¦h®Ö¤ß¡A¦Ó¥B§Aªº¨t²Î¤]¥²¶·¥ý¦w¸Ë¥­¦æ³B²z¤u¨ã½c¡]Parallel Processing Toolbox¡A²ºÙ PCT¡^¡C­Y¨âªÌ­Ý³Æ¡A¥[³t­pºâªº¤èªk¦p¤U¡G
  1. ¦b¨Ï¥Î¥­¦æ³B²z«ü¥O¤§«e¡A¤U¹F¡umatlabpool local n¡v«ü¥O¡A¨ä¤¤ n ¥Nªí®Ö¤ß¼Æ¥Ø¡A¨Ò¦p 4¡]¥|®Ö¤ß¹q¸£¡^©Î 8¡]¤K®Ö¤ß¹q¸£¡^¡C
  2. ¨Ï¥Î parfor «ü¥O¨Ó¨ú¥N­ì¨Óªº for °j°é¡A¨ä«ü¥O®æ¦¡¦p¤U¡G
    parfor loopvar = initval:endval, statements, end
  3. ³Ì«á¤U¹F¡umatlabpool close¡v¨ÓÃö³¬¥­¦æ³B²zªº¥\¯à¡C
§Ú­Ì´N¥i¥H¨Ó¹Á¸Õ¤@­Ó²³æªº½d¨Ò¡G

Example 1: 02-µ{¦¡½X»P°O¾ÐÅ餧³Ì¨Î¤Æ/parfor01.mver distcomp % Åã¥Ü PCT ªºª©¥» matlabpool local 4 n = 100; rowMedian1=zeros(1000, 1); rowMedian2=zeros(1000, 1); % === ²Ä¤@ºØ¤èªk¡Gcommon for-loop tic for i = 1:1000 rowMedian1(i) = max(eig(rand(n))); end time1 = toc; % === ²Ä¤GºØ¤èªk¡Gparallep for-loop tic parfor i = 1:1000 rowMedian2(i) = max(eig(rand(n))); end time2 = toc; fprintf('time1 = %g, time2 = %g, speedup factor = %g\n', time1, time2, time1/time2); matlabpool close------------------------------------------------------------------------------------------------------- MATLAB Version: 8.1.0.604 (R2013a) MATLAB License Number: DEMO Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1) Java Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode ------------------------------------------------------------------------------------------------------- Parallel Computing Toolbox Version 6.2 (R2013a) Starting matlabpool using the 'local' profile ... connected to 4 workers. time1 = 4.94823, time2 = 1.62187, speedup factor = 3.05094 Sending a stop signal to all the workers ... stopped.

¦b³o­Ó½d¨Ò¤¤¡A§Ú­Ì²£¥Í¤F 1000 ­Ó 100x100 ªº¶Ã¼Æ¯x°}¡AµM«á­pºâ¨C¤@­Ó¯x°}ªº¯S¼x­È¡]¦@¦³ 100­Ó¡^ªº³Ì¤j­È¡A­Y¨Ï¥Î¥­¦æ³B²z±M¥Îªº parfor °j°é¡A­pºâ³t«×¬O¤@¯ë for °j°éªº 3 ­¿¥ª¥k¡C¡]§Úªº¹q¸£¬O¥|®Ö¤ß¡C¡^

¦pªG§A¦b°õ¦æ MATLAB ®É¡A¤@ÃäÁÙ­nÁÙ­n­×§ï½×¤å¡A¦¹®É­Y MATLAB ¦û¥Î¤F©Ò¦³ªº¦h®Ö CPU¡A¥i¯à·|¾É­P¹q¸£¾ã­ÓºC¤U¨Ó¡AÅý§AµLªk¶i¦æ¤å®Ñ½s¿è¡C¦]¦¹ parfor ´£¨Ñ¥t¤@­Ó°Ñ¼Æ¡A¥i¥H«ü©w©Ò¥Î¨ìªº¤u§@ªÌ­Ó¼Æ¡A«ü¥O®æ¦¡¦p¤U¡G

parfor (loopvar = initval:endval, M), statements, end
¨ä¤¤ M §Y¬O©Ò¥Î¨ìªº®Ö¤ß­Ó¼Æ¡C¨Ò¦p¦b¤U¨Ò¤¤¡AÁöµM§Úªº¹q¸£¬O¥|®Ö¤ß¡A¦ý§Ú¥u¨Ï¥Î¨â®Ö¨Ó¶i¦æ parfor ªº¹Bºâ¡G

Example 2: 02-µ{¦¡½X»P°O¾ÐÅ餧³Ì¨Î¤Æ/parfor02.mfprintf('MATLAB version = %s\n', version); matlabpool local 4 n = 100; rowMedian1=zeros(1000, 1); rowMedian2=zeros(1000, 1); % === ²Ä¤@ºØ¤èªk¡Gcommon for-loop tic for i = 1:1000 rowMedian1(i) = max(eig(rand(n))); end time1 = toc; % === ²Ä¤GºØ¤èªk¡Gparallel for-loop using 2 workers tic parfor (i = 1:1000, 2) % ¥u¥Î¨â­Ó®Ö¤ß rowMedian2(i) = max(eig(rand(n))); end time2 = toc; fprintf('time1 = %g, time2 = %g, speedup factor = %g\n', time1, time2, time1/time2); matlabpool closeMATLAB version = 8.1.0.604 (R2013a) Starting matlabpool using the 'local' profile ... connected to 4 workers. time1 = 4.91713, time2 = 2.58577, speedup factor = 1.90162 Sending a stop signal to all the workers ... stopped.

¥Ñ©ó¥u¥Î¨ì¨â®Ö¡A©Ò¥H¥[³t­¿¼Æ±µªñ¨â­¿¡C

¦b¨Ï¥Î parfor °j°é®É¡A¦³´XÂI­n¯S§Oª`·N¡G

PCT ¬O¤@­Ó¥\¯à±j¤jªº¤u¨ã½c¡A¥Ø«e¤]¥i¥H¤ä´©¦h¥x¹q¸£ªº¥­¦æ¹Bºâ¡A¨Ã¥i¥H¨Ï¥Î GPU¡]Graphic Processing Unit¡A¹Ï§Î³B²z¾¹¡^¨Ó¶i¦æ¤j¶qªº¥­¦æ¹Bºâ¡A¸Ô²Ó¥\¯à½Ð°Ñ¦Ò¤U¤@¤p¸`ªº¤¶²Ð¡C


MATLABµ{¦¡³]­p¡G¶i¶¥½g